package com.plant.serviceImpl;

import com.plant.dao.BaseDao;
import com.plant.entity.UgcFaq;
import com.plant.entity.UgcFaqAnswer;
import com.plant.entity.UgcUploadImages;
import com.plant.entity.UgcUploadImagesTags;
import com.plant.entity.UgcUser;
import com.plant.service.HelpService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service                                                                        //依赖注册 必须写注册 下面在写resource资源
@Transactional
public class HelpServiceImpl implements HelpService{
    @Resource(name = "baseDaoImpl")      //注解扫描basedao接口方法
    private BaseDao baseDao;
    
    public List<Long> helpCount(UgcUser uf) {
    	//1.下面是统计条数的方法实现
        String hql="select count(id) from UgcFaq where userId=("+uf.getId()+")"; //统计我的求助条数
        Long mycount=baseDao.CountHql(hql);
        //2.统计我的回答条数
        String hql4="select count(id) from UgcFaqAnswer where userId=("+uf.getId()+")";
        Long answercount=baseDao.CountHql(hql4);
        //3.统计我的好友求助条数
        String hql2="select count(id) from UgcFaq where userId in (select applyUserId from UgcFriends where receiveUserId=("+uf.getId()+"))";
        String hql22="select count(id) from UgcFaq where userId in (select receiveUserId from UgcFriends where applyUserId=("+uf.getId()+"))";
        Long friendcount=baseDao.CountHql(hql2)+baseDao.CountHql(hql22);
        //4.统计不是我的好友的求助条数
        String hql3="select count(id) from UgcFaq";
        Long notfriendcount=baseDao.CountHql(hql3)-friendcount-mycount;
        List<Long>  list=new ArrayList<Long>();
        list.add(mycount);
        list.add(friendcount);
        list.add(notfriendcount);
        list.add(answercount);
        return list;
    }

	@SuppressWarnings("unchecked")
	public List<Object[]> myFaq(UgcUser uf,Integer page,String time) {  //1.显示我的求助
    	List<Object[]> list=new ArrayList<Object[]>();
    	String hql="select a.id,a.title,a.create_time from ugc_faq as a where a.user_id=("+uf.getId()+") and a.create_time<? order by a.create_time desc";
    	list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcFaq.class);
    	return list;
    }
    
	public Long faqcount(UgcUser uf,Object obj) {//---统计我的求助好友回答我的数量
		String hql="select count(id) from UgcFaqAnswer where userId in(select applyUserId from UgcFriends where receiveUserId=("+uf.getId()+")) and toFaqId=("+obj+"))";
		Long lo=baseDao.CountHql(hql);
		return lo;
	}
	
	public Long nofriendcount(UgcUser uf,Object obj) {//---统计陌生人的回答条数数量
		String hql="select count(id) from UgcFaqAnswer where userId not in(select applyUserId from UgcFriends where receiveUserId=("+uf.getId()+")) and toFaqId=("+obj+"))";
		Long lo=baseDao.CountHql(hql);
		return lo;
	}
	
    @SuppressWarnings("unchecked")
	public List<Object[]> frendFaq(Object Obj,Integer page,String time) {  //2.显示好友求助
    	List<Object[]> list=new ArrayList<Object[]>();
    	String hql="select a.id,a.user_id,a.title,a.create_time,c.nickname,c.photo from ugc_faq as a,ugc_user as c where a.user_id=c.id and a.user_id in ("+Obj+") and a.create_time<? order by a.create_time desc";
        list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcFaq.class);
        return list;
    }
    
	public Long friendfaqcount(UgcUser uf,Object obj) {//----求助回答的数量
		String hql="select count(id) from UgcFaqAnswer where toFaqId=("+obj+"))";
		Long lo=baseDao.CountHql(hql);
		return lo;
	} 
	
	@SuppressWarnings("unchecked")
	public Object friendfaqmyanswer(UgcUser uf,Object obj) {//---2.1我回答朋友或是陌生人--求助的内容
		 List<Object> list=new ArrayList<Object>();
		String hql="select content from UgcFaqAnswer where toFaqId=("+obj+") and userId=("+uf.getId()+")";
		list=baseDao.find(hql);
		Object ob=null;
		for (int j = 0; j < list.size(); j++) {
			ob = list.get(0);
		}
		return ob;
	} 
	
	
    @SuppressWarnings("unchecked")
	public List<Object[]> strangerFaq(Object obj,UgcUser uf,Integer page,String time) { //3.陌生人的求助
    	List<Object[]> list=new ArrayList<Object[]>();
    	String hql="select a.id,a.user_id,a.title,a.create_time,c.nickname,c.photo from ugc_faq as a,ugc_user as c where a.user_id=c.id and a.user_id not in("+obj+") and a.user_id<>("+uf.getId()+")and a.create_time<? order by a.create_time desc";
        list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcFaq.class);
        return list;
    }
	
    @SuppressWarnings("unchecked")
	public List<Object[]> myanswer(UgcUser uf,Integer page,String time){ //---4.查看我的回答
    	List<Object[]> list=new ArrayList<Object[]>();
    	String hql="select a.id,a.create_time,a.content,a.to_faq_id,b.title,a.user_id,c.nickname,c.photo from ugc_faq_answer as a,ugc_faq as b,ugc_user as c where a.to_faq_id=b.id and c.id=a.user_id and a.user_id=("+uf.getId()+") and a.create_time<? order by a.create_time desc";
    	list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcFaqAnswer.class);
        return list;
    }
	@SuppressWarnings("unchecked")
	public List<Object[]> myanswerI(UgcUser uf,Integer page,String time){
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select a.id,a.create_time,a.content,a.to_faq_id,b.title,a.user_id,c.nickname,c.photo from ugc_faq_answer as a,ugc_faq as b,ugc_user as c where a.to_faq_id=b.id and c.id=b.user_id and a.user_id=("+uf.getId()+") and a.create_time<? order by a.create_time desc";
		list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcFaqAnswer.class);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public Object oneurl(UgcUser uf,Object obj) {//---查看我的回答 的一张图片显示
		List<Object> list=new ArrayList<Object>();
		String hql="select url from ugc_upload_images where from_content_id=("+obj+") and from_content_type=3 limit 0,1"; 
		list=baseDao.Bysql(hql, UgcUploadImages.class);
		Object ob=null;
		for (int j = 0; j < list.size(); j++) {
			ob = list.get(0);
		}
		return ob;
	}

	public boolean addfaq(UgcFaq fq) {  //----用户发布求助
		return baseDao.add(fq);
	} 
	public boolean addfaqphoto(UgcUploadImages fqp) {  //----用户发布求助图片到 用户上传图片数据库
		return baseDao.add(fqp);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> myfrienduid(UgcUser uf) {    //查询我的好友的所有用户的id
		String hql="select * from ugc_friends where receive_user_id<>("+uf.getId()+") and apply_user_id<>("+uf.getId()+")";
		return baseDao.Bysql(hql, UgcUser.class);
	}

	public Long FriendFaq(Object obj) {
        String hql2="select count(id) from UgcFaq where userId=("+obj+"))";
        Long friendcount=baseDao.CountHql(hql2);
        return friendcount;
	}

	@SuppressWarnings("unchecked")
	public List<Object> GetFriendUidByA(UgcUser uf) {
		String hql="select apply_user_id from ugc_friends where receive_user_id=("+uf.getId()+")";
		return baseDao.Bysql(hql, UgcUser.class);
	}
	public List<?> GetNotFriendUid(UgcUser uf) {
		String hql="select apply_user_id from ugc_friends where receive_user_id<>("+uf.getId()+")";
		return baseDao.Bysql(hql, UgcUser.class);
	}

	@SuppressWarnings("unchecked")
	public List<Object> GetFriendUidByR(UgcUser uf) {
		String hql="select receive_user_id from ugc_friends where apply_user_id=("+uf.getId()+")";
		return baseDao.Bysql(hql, UgcUser.class);
	}

	public boolean AddFaqAnswer(UgcFaqAnswer ufa) {//---添加求助回答
		return baseDao.add(ufa);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> AnswerHelpList(UgcFaqAnswer ufa,Integer page,String time) {//---查看显示求助回答
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select a.id,a.user_id,a.content,to_faq_id,a.create_time,b.nickname,b.photo,a.from_platform from ugc_faq_answer as a,ugc_user as b where a.user_id=b.id and a.to_faq_id=("+ufa.getToFaqId()+") and a.create_time<? order by a.create_time desc";
    	list=baseDao.findfenye((page-1)*10, 10, hql, time,UgcFaqAnswer.class);
        return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> HelpInfo(UgcFaq uf) {//----求助的单个详情
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select a.id,a.user_id,a.remind_user_id,a.title,a.content,a.create_time,b.nickname,b.photo from ugc_faq as a,ugc_user as b where a.user_id=b.id and a.id=("+uf.getId()+")";
		list=baseDao.Bysql(hql, UgcFaq.class);
		return list;
	}

	public Object PartUserInf(UgcUser u) {
		return baseDao.find(UgcUser.class, u.getId());
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> HelpInfImagesList(Object obj) {//----查询求助图片
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select id,url,user_id from ugc_upload_images where from_content_id=("+obj+") and from_content_type=3";
		list=baseDao.Bysql(hql, UgcFaq.class);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> HelpInfImagesListS(Object obj) {//----查询求助图片
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select url from ugc_upload_images where from_content_id=("+obj+") and from_content_type=3";
		list=baseDao.Bysql(hql, UgcFaq.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> HelpInfImagesTags(Object objuid,Object objimid) {//---查询显示图片标签
		String hql="select id,x,y,rediret,text from ugc_upload_images_tags where from_image_id=("+objimid+") and user_id="+objuid+"";
		return baseDao.Bysql(hql, UgcFaq.class);
	}

	public Long FaqAnswerCount(Object obj) {//----统计回答个数
		String hql="select count(id) from UgcFaqAnswer where toFaqId="+obj+"";
		return baseDao.CountHql(hql);
	}

	@SuppressWarnings("unchecked")
	public List<String> UserFriendToken(String ids) {
		String hql="select divice_token from ugc_user where id in ("+ids+")";
		return baseDao.Bysql(hql, UgcFaq.class);
	}

	public boolean AddTags(UgcUploadImagesTags ta) {//----添加标签
		return baseDao.add(ta);
	}

	@SuppressWarnings("unchecked")
	public List<String> AllUserDeviceToken(UgcUser u) {
		String hql="select divice_token from ugc_user where id not in ("+u.getId()+")";
		return baseDao.Bysql(hql, UgcUser.class);
	}

	@SuppressWarnings("unchecked")
	public boolean CheckFaqAnswerLikes(Integer user_id, Integer to_user_id,
			Integer to_content_id) {
		List<Object> list=new ArrayList<Object>();
		String sql="SELECT id FROM ugc_like WHERE user_id="+user_id+" AND to_user_id="+to_user_id+" AND to_content_id="+to_content_id+" and to_content_type=3";
		list=baseDao.Bysql(sql, UgcFaqAnswer.class);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}

	public Long FaqLikeCount(Object obj) {//统计求助回答点赞数
		String hql="select count(id) from UgcLike where toContentId="+obj+" and toContentType=3";
		return baseDao.CountHql(hql);
	}

	public Long OneFaqAnswerCount(Object obj) {
		String hql="select count(id) from UgcFaqAnswer where toFaqId="+obj+"";
		return baseDao.CountHql(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> AllFaq(String time) {
		String sql="select id id_key,title from ugc_faq where create_time<'"+time+"'";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Integer> findfaqid(UgcFaq uf) {
		String sql="SELECT id FROM ugc_faq WHERE user_id="+uf.getUserId()+" ORDER BY create_time DESC LIMIT 1";
		return baseDao.Bysql(sql, UgcFaq.class);
	} 
	
	
}
